<?php

namespace App\EventSubscriber;

use App\Entity\Organization;
use App\Event\OrganizationRemoveEvent;
use App\Utils\Helper\GetterHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * 组织架构删除事件订阅
 * Class OrganizationRemoveSubscriber
 * @package App\EventSubscriber
 */
class OrganizationRemoveSubscriber implements EventSubscriberInterface {
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents () {
        return [
            OrganizationRemoveEvent::NAME => [
                ['removeChildNode', 10],
            ],
        ];
    }

    /**
     * 删除子节点
     * @param OrganizationRemoveEvent $event
     */
    public function removeChildNode (OrganizationRemoveEvent $event) {
        $organization_id = $event->getOrganizationId();
        $ids = GetterHelper::getEntityManager()->getRepository(Organization::class)->getChildNodes($organization_id);
        GetterHelper::getEntityManager()->getRepository(Organization::class)->batchRemove($ids);
    }

}